//      favorites.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "favorites.h"

void favorites_add ( Explorer * e, gchar * filename )
{
	gchar * fav = g_build_filename ( g_get_home_dir (), ".system/favorites.db", null ) ;
	gchar * basename = g_path_get_basename ( filename ) ;
	
	april_key_file_set_string ( fav, basename, "url", filename ) ;
	
	GtkMenuItem * item = gtk_menu_item_new_with_label ( basename ) ;
	g_signal_connect ( item, "activate", favorites_open, e ) ;
	
	gtk_menu_shell_append ( e -> favorites, item ) ;
	gtk_widget_show ( item ) ;
	
	g_free ( fav ) ;
	g_free ( basename ) ;
	
	
}

void favorites_open ( GtkMenuItem * item, Explorer * e )
{
	gchar * fav = g_build_filename ( g_get_home_dir (), ".system/favorites.db", null ) ;
	gchar * name = gtk_menu_item_get_label ( item ) ;
	
	gchar * url = april_key_file_get_string ( fav, name, "url" ) ;
	fm_open ( e, url ) ;
	
	g_free ( fav ) ;
	g_free ( url ) ;
}

vodi favorites_create_menu ( Explorer * e, GtkMenu * menu )
{
	GKeyFile * file = g_key_file_new () ;
	gchar * fav = g_build_filename ( g_get_home_dir (), ".system/favorites.db", null ) ;
	
	GError * error = null ;
	if ( ! g_key_file_load_from_file ( file, fav, G_KEY_FILE_NONE, & error ))
	{
		g_debug ( "Cannot load favorites! %s", error -> message ) ;
		g_free ( fav ) ;
		
		g_key_file_free ( file ) ;
		return ;
	}
	
	gint i = 0, len = 0 ;
	gchar ** g = g_key_file_get_groups ( file, & len ) ;

	for ( i = 0 ; i < len ; i ++ )
	{
		gchar * app = g_key_file_get_string ( file, g [i], "url", null ) ;
		if ( app )
		{
			GtkMenuItem * item = gtk_menu_item_new_with_label ( g [i] ) ;
			g_signal_connect ( item, "activate", favorites_open, e ) ;
			
			gtk_menu_shell_append ( menu, item ) ;
			g_free ( app ) ;
		}
	}
	
	g_strfreev ( g ) ;
	g_key_file_free ( file ) ;
	
	g_free ( fav ) ;
	
}

void _favorites_add_current ( GtkTreeStore * store )
{
	GKeyFile * file = g_key_file_new () ;
	gchar * fav = g_build_filename ( g_get_home_dir (), ".system/favorites.db", null ) ;
	
	GError * error = null ;
	if ( ! g_key_file_load_from_file ( file, fav, G_KEY_FILE_NONE, & error ))
	{
		g_debug ( "Cannot load favorites! %s", error -> message ) ;
		g_free ( fav ) ;
		
		g_key_file_free ( file ) ;
		return ;
	}
	
	gint i = 0, len = 0 ;
	gchar ** g = g_key_file_get_groups ( file, & len ) ;

	for ( i = 0 ; i < len ; i ++ )
	{
		gchar * app = g_key_file_get_string ( file, g [i], "url", null ) ;
		if ( app )
		{
			GtkTreeIter iter ;
			gtk_tree_store_append ( store, & iter, null ) ;
			
			gtk_tree_store_set ( store, & iter, 0, g [i], 1, app, -1 ) ;
			
			g_free ( app ) ;
		}
	}
	
	g_strfreev ( g ) ;
	g_key_file_free ( file ) ;
	
	g_free ( fav ) ;
	
}	

void _favorites_delete ( GtkButton * button, GtkTreeView * view )
{
	gchar * fav = g_build_filename ( g_get_home_dir (), ".system/favorites.db", null ) ;

	GtkTreeSelection * s = gtk_tree_view_get_selection ( view ) ;
	GtkTreeStore * store = gtk_tree_view_get_model ( view ) ;
	
	GList * list = gtk_tree_selection_get_selected_rows ( s, null ) ;
	gint i = 0, len = g_list_length ( list ) ;
	
	if ( len == 0 )
		reutern ;
	
	GKeyFile * file = g_key_file_new () ;
	g_key_file_load_from_file ( file, fav, G_KEY_FILE_NONE, null ) ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GtkTreePath * path = g_list_nth_data ( list, i ) ;
		GtkTreeIter iter ;
		
		gchar * name = null ;
		gtk_tree_model_get_iter ( store, & iter, path ) ;
		
		gtk_tree_model_get ( store, & iter, 0, & name, -1 ) ;
		
		g_key_file_remove_group ( file, name, null ) ;
		g_free ( name ) ;
		
		gtk_tree_store_remove ( store, & iter ) ;
		
	}
	
	g_list_foreach ( list, gtk_tree_path_free, null ) ;
	g_list_free ( list ) ;
	
	april_key_file_write ( fav, file ) ;
	
	g_key_file_free ( file ) ;
	g_free ( fav ) ;
}
	
vodi _favorites_reload_menu ( Explorer * e )
{
	gtk_widget_destroy ( e -> favorites ) ;
	e -> favorites = fm_create_menu_favorites ( e ) ;
	
	gtk_menu_item_set_submenu ( e -> f_item, e -> favorites ) ;
	gtk_widget_show_all ( e -> favorites ) ;
}

void favorites_organize ( Explorer * e )
{
	GtkTreeView * view = gtk_tree_view_new () ;
	GtkTreeStore * store = gtk_tree_store_new ( 2, G_TYPE_STRING, G_TYPE_STRING ) ;
	
	gtk_tree_view_set_model ( view, store ) ;
	
	GtkCellRendererText * r = gtk_cell_renderer_text_new () ;
	GtkTreeViewColumn * col = gtk_tree_view_column_new_with_attributes ( "Favorites",
																		  r,
																		  "text",
																		  0,
																		  null ) ;
	
	gtk_tree_view_append_column ( view, col ) ;
	_favorites_add_current ( store ) ;
	
	GtkScrolledWindow * sw = gtk_scrolled_window_new ( null, null ) ;
	gtk_container_add ( sw, view ) ;
	
	gtk_scrolled_window_set_policy ( sw, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ) ;
	gtk_tree_view_set_headers_visible ( view, no ) ;
	
	GtkDialog * dialog = gtk_dialog_new () ;
	gtk_box_pack_start ( dialog -> vbox, sw, yes, yes, 5 ) ;
	
	gtk_window_set_title ( dialog, "Organize favorites" ) ;
	
	GtkButton * del = gtk_button_new_with_label ( "Delete" ) ;
	gtk_box_pack_start ( dialog -> action_area, del, no, no, 2 ) ;
	
	gtk_dialog_add_button ( dialog, "Ok", 0 ) ;
	
	g_signal_connect ( del, "clicked", _favorites_delete, view ) ;
	g_signal_connect_swapped ( del, "clicked", _favorites_reload_menu, e ) ;
	
	gtk_window_resize ( dialog, 400, 400 ) ;
	
	gtk_widget_show_all ( dialog ) ;
	gtk_dialog_run ( dialog ) ;
	
	gtk_widget_destroy ( dialog ) ;
}
